package com.fish.yssb.ui.page.activity.wave;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.api.retData;
import com.fish.yssb.api.setApi;
import com.fish.yssb.db.RealmDb;
import com.fish.yssb.db.bean.BaseInfoTable;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.adapter.ctrlAdapter;
import com.fish.yssb.ui.page.util.TopToastUtil;
import com.jaeger.library.StatusBarUtil;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Date;


/**
 * A login screen that offers login via email/password.
 */
public class CtrlActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = CtrlActivity.class.getSimpleName();

    LinearLayout tv_back;
    TextView tv_toptitle,tv_set;

    private RecyclerView recyclerView;
    private ctrlAdapter adapter;
    private CircleProgressDialog circleProgressDialog;

    private RelativeLayout ll_para_1, ll_para_2, ll_para_3;
    private static int m_type;
    private static String m_title;

    public static void start(Context context) {
        m_title = "控制传动";
        Intent intent = new Intent(context, CtrlActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_ctrl);
        initview();
        EventBus.getDefault().register( this );
        circleProgressDialog = new CircleProgressDialog(CtrlActivity.this);
        circleProgressDialog.setText("命令执行中");
    }




    @Subscribe
    public void onEvent(final retData result) {
        //接收以及处理数据
        runOnUiThread(new Runnable() {
            public void run() {
                int strType = result.getType();
                int ret = result.getRet();
                int status = result.getStatus();

                if(strType == Consts.cm_w_ctrl || strType == Consts.cm_w_ctrl_2)
                {
                    circleProgressDialog.dismiss();
                    if(ret == Consts.cm_timeout) {
                        TopToastUtil.getInstance(CtrlActivity.this, R.layout.showdialog).showShortToast("命令执行超时");
                    }

                    if(ret == Consts.cm_end) {
                        if(status == 1) {
                            TopToastUtil.getInstance(CtrlActivity.this, R.layout.showdialog).showShortToast("命令执行成功");
                        }else{
                            TopToastUtil.getInstance(CtrlActivity.this, R.layout.showdialog).showShortToast("命令执行失败");

                        }
                    }
                }

            }
        });




    };



    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层
        recyclerView = (RecyclerView) findViewById(R.id.recylerview);
        recyclerView.setHasFixedSize(true);

        tv_toptitle = findViewById(R.id.tv_toptitle);
        tv_toptitle.setText(m_title);

        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter = new ctrlAdapter());

        adapter.replaceAll(getData(),CtrlActivity.this);

        tv_back = findViewById(R.id.tv_back);
        tv_back.setOnClickListener(this);
        tv_set = findViewById(R.id.tv_set);
        tv_set.setVisibility(View.INVISIBLE);
        tv_set.setOnClickListener(this);


    }

    public void setData(short type,boolean flag )
    {
        int ret =  setApi.getInstance(CtrlActivity.this).tv_w_sub_ctrl(type,flag);
        if(ret ==1)
        {
            circleProgressDialog.showDialog();
        }else{
            TopToastUtil.getInstance(CtrlActivity.this,R.layout.showdialog).showShortToast("发送命令失败");

        }
    }

    public void setData_1(short type,boolean flag )
    {
        int ret = setApi.getInstance(CtrlActivity.this).tv_w_sub_ctrl_1(type,flag);
        if(ret ==1)
        {
            circleProgressDialog.showDialog();
        }else{
            TopToastUtil.getInstance(CtrlActivity.this,R.layout.showdialog).showShortToast("发送命令失败");

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister( this );
    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_back:
                finish();
                break;
            case R.id.tv_set:
                break;
            default:
                break;
        }


    }

    public ArrayList<BaseInfoTable> getData()
    {
//        ArrayList<BaseInfoTable> list = (ArrayList<BaseInfoTable>) RealmDb.getInstance().queryRealmObjects(BaseInfoTable.class);

//        Log.e("=========","count:"+list.size());
        ArrayList<BaseInfoTable> arrayList = (ArrayList) RealmDb
                .getInstance()
                .queryRealmObjectsByData(BaseInfoTable.class,"paraType",4);
        return arrayList;
    }

}

